package com.atguigu.realtime.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/2/7 21:38
 */
public class MyRedisUtil {
    public static JedisPool jedisPool = null;
    
    public static Jedis getClient() {
        
        if (jedisPool == null) {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(300); //最大可用连接数
            jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
            jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
            
            jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
            jedisPoolConfig.setMinIdle(5); //最小闲置连接数
            
            jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong
            
            jedisPool = new JedisPool(jedisPoolConfig, "hadoop162", 6379, 5000);
            System.out.println("开辟连接池");
            
        } else {
            System.out.println(" 连接池:" + jedisPool.getNumActive());
        }
        return jedisPool.getResource();
    }
}
